<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17/12/26
 * Time: 11:12
 */


class MeetingModel
{
    public function liveListsAction($fid,$page,$number=PAGE_NUMBER)
    {
        $data['lists']  = Db::name('meeting')
            ->field('m.id as mid,m.name,left(m.play_time, 16) as play_time,if(m.password,1,0) as password,m.click,ud.nickname,ud.avatar')->alias('m')
            ->join('users_detail ud','m.udid = ud.id')
            ->where(['m.fid'=>$fid,'m.status'=>2])
            ->page($page,$number)
            ->order('m.id desc')
            ->select();
        $data['page'] = $page;
        $data['count'] = Db::name('meeting')->where(['fid'=>$fid,'status'=>1])->count();
        return $data;
    }

    public function liveAction($user,$mid)
    {
        $data = Db::name('meeting')
            ->field('m.id as mid,m.udid,m.name,left(m.play_time, 16) as play_time,m.channel,m.click,ud.nickname,ud.avatar')->alias('m')
            ->join('users_detail ud','m.udid = ud.id')
            ->where(['m.fid'=>$user['fid'],'m.status'=>2,'m.id'=>$mid])
            ->order('m.id desc')
            ->find();
        if($data) {
            $play = Db::name('meeting')
                ->field('SEC_TO_TIME(TIMESTAMPDIFF(SECOND,play_time,NOW())) as play')
                ->where(['id' => $data['mid'], 'fid' => $user['fid']])
                ->find();
            $data['play'] = $play['play'];
            
            $where['mid'] = $data['mid'];
            $where['status']=2;
            $where['fid']=$data['fid'];
            $mikes = Db::name('mike')->field('udid')->where($where)->select();
            $data['mikes'] = $mikes;
        }
        return $data;
    }

    public function backListsAction($fid,$page,$number=PAGE_NUMBER)
    {
        $data['lists']  = Db::name('meeting')
            ->field('m.id as mid,m.name,left(m.play_time, 16) as play_time,if(m.password,1,0) as password,m.click,ud.nickname,ud.avatar')->alias('m')
            ->join('users_detail ud','m.udid = ud.id')
            ->where(['m.fid'=>$fid,'m.status'=>9])
            ->page($page,$number)
            ->order('m.id desc')
            ->select();
        $data['page'] = $page;
        $data['count'] = Db::name('meeting')->where(['fid'=>$fid,'status'=>9])->count();
        return $data;
    }

    public function backAction($user,$mid)
    {
        $data = Db::name('meeting')
            ->field('m.id as mid,m.name,left(m.play_time, 16) as play_time,m.channel,m.viewing,m.video,ud.nickname,ud.avatar')->alias('m')
            ->join('users_detail ud','m.udid = ud.id')
            ->where(['m.fid'=>$user['fid'],'m.status'=>9,'m.id'=>$mid])
            ->order('m.id desc')
            ->find();
        return $data;
    }

    public function passwordAction($content,$user)
    {
        $data = Db::name('meeting')
            ->where(['id'=>$content['mid'],'password'=>$content['password'],'fid'=>$user['fid']])
            ->find();
        return $data;
    }

    public function addWatchAction($mid,$user,$status)
    {
        if($status == 1){
            Db::name('meeting')->where(['id'=>$mid,'fid'=>$user['fid'],'status'=>1])->inc('click')->setInc('viewing');
        }
        if($status == 9){
            Db::name('meeting')->where(['id'=>$mid,'fid'=>$user['fid'],'status'=>9])->setInc('viewing');
        }
    }

    public function delAction($user,$mid)
    {
        $where['id'] =$mid;
        $where['fid'] =$user['fid'];
        $where['status'] =9;
        $del = Db::name('meeting')->where($where)->update(['status'=>0]);
        if($del){
            $msg['message'] = '会议删除成功';
        }else{
            $msg['message'] = '会议删除失败';
        }
        return $msg;
    }


    public function usersAction($udid,$mid)
    {
        $users = Db::name('users_detail')
            ->field('ud.id as udid,ud.phone,ud.nickname,ud.section,ud.duties,ud.avatar,ifnull(m.status,0) as status')->alias('ud')
            ->join('mike m','m.udid = ud.id AND mid = '.$mid,'LEFT')
            ->where(['ud.id'=>['in',$udid],'m.status'=>['in','1,2']])
            ->order('ud.nickname ASC')
            ->select();
        return $users;
    }
}

